package com.zb.api.business.service.impl;

import com.zb.api.business.entity.PosMerchantEntity;
import com.zb.api.business.entity.UserEntity;
import com.zb.api.business.mapper.PosMerchantMapper;
import com.zb.api.business.mapper.UserMapper;
import com.zb.api.business.service.UserService;
import com.zb.api.common.econts.DecorationStyleEnum;
import com.zb.api.common.models.UserVo;
import com.zb.api.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;


/**
 * 后台用户管理
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PosMerchantMapper posMerchantMapper;

    @Override
    public UserEntity Sel(int id) {
        int i = 1;
        return userMapper.Sel(id);
    }

    @Override
    public UserEntity getUserByUserNameAndPassword(UserEntity userEntity) {
        return userMapper.getUserByUserNameAndPassword(userEntity);
    }

    /**
     * 根据用户名判断用户名是否被使用
     * @param username
     * @return
     */
    @Override
    public UserEntity getUserByUserName(String username) {
        return userMapper.getUserByUserName(username);
    }

    @Override
    public UserEntity getUserInfo(UserEntity userEntity) {
        //UserEntity user =  UserThreadLocal.get();
        UserEntity user =  userMapper.getInfo(userEntity.getId());
        PosMerchantEntity posMerchant = posMerchantMapper.getMerchant(user.getId());
        if (posMerchant!=null){
            user.setIfMerchant(1);
            user.setMerchantId(posMerchant.getId());
            user.setMerchantName(posMerchant.getPosName());
        }
        if (user.getRemarks()!=null){
            List<String> styles = Arrays.asList(user.getRemarks().split(","));
            String name = "";
            for (String style : styles) {
                name=name+DecorationStyleEnum.value(Integer.parseInt(style))+",";
            }
            if (!StringUtils.isEmpty(name)){
                name.substring(0,name.length()-1);
            }
            user.setRemarksName(name);
        }
        return user;
    }

    @Override
    public int userRegister(UserVo userVo) {

        return userMapper.userRegister(userVo);
    }

    @Override
    public int updateUserInfo(UserVo userVo) {
        return userMapper.updateUserInfo(userVo);
    }

    @Override
    public int updateUserPasswordById(String password, Integer id) {
        return userMapper.updateUserPasswordById(password,id);
    }


}
